package com.codeoss.framework.webide.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeoss.framework.webide.request.DevFormDesignerRequest;
import com.codeoss.framework.webide.response.DevFormDesignerResponse;
import com.codeoss.framework.webide.service.IDevFormDesignerService;

@Controller
@RequestMapping("devformdesigner")
public class DevFormDesignerController {
	@Autowired
	private IDevFormDesignerService devFormDesignerService;
	
	@RequestMapping(value = "init", method = RequestMethod.GET)
	@ResponseBody
	public DevFormDesignerResponse init(@RequestParam("pageCode")String pageCode){
		DevFormDesignerResponse devFormDesignerResponse=devFormDesignerService.init(pageCode);
		return devFormDesignerResponse;
	}
	
	@RequestMapping(value = "dataSave", method = RequestMethod.POST)
	@ResponseBody
	public DevFormDesignerResponse dataSave(@RequestBody DevFormDesignerRequest request){
		DevFormDesignerResponse devFormDesignerResponse=devFormDesignerService.dataSave(request);
		return devFormDesignerResponse;
	}
	
	@RequestMapping(value = "dataExport", method = RequestMethod.GET)
	public void dataExport(@RequestParam("pageCode")String pageCode,HttpServletResponse response) throws IOException{
		DevFormDesignerResponse devFormDesignerResponse=devFormDesignerService.dataExport(pageCode);
		response.reset();
		try {
			response.addHeader("Content-Disposition","attachment;filename="+new String((devFormDesignerResponse.getFileName()).getBytes(),"ISO-8859-1"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.getWriter().write(devFormDesignerResponse.getFileContent());
	}
	
	@RequestMapping(value = "initColumnConfig", method = RequestMethod.GET)
	@ResponseBody
	public DevFormDesignerResponse initColumnConfig(@RequestParam("tableName")String tableName,@RequestParam("editable")boolean editable){
		DevFormDesignerResponse devFormDesignerResponse=devFormDesignerService.initColumnConfig(tableName, editable);
		return devFormDesignerResponse;
	}
	
	@RequestMapping(value = "getChartConfig", method = RequestMethod.GET)
	@ResponseBody
	public DevFormDesignerResponse getChartConfig(@RequestParam("tableName")String tableName){
		DevFormDesignerResponse devFormDesignerResponse=devFormDesignerService.getChartConfig(tableName);
		return devFormDesignerResponse;
	}
}
